package com.tao.lab.utils

/**
 *
 * @ProjectName:    ros
 * @Package:        com.xxl.roslib.base
 * @ClassName:      SingletonHolder
 * @Description:     java类作用描述
 * @Author:         wangxf
 * @CreateDate:     2022/3/4 17:50
 * @UpdateUser:     更新者
 * @UpdateDate:     2022/3/4 17:50
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
open class SingleParam<out T, in A>(creator: (A) -> T) {
    private var creator: ((A) -> T)? = creator

    @Volatile
    private var instance: T? = null

    fun getInstance(arg: A): T {
        val i = instance
        if (i != null) {
            return i
        }

        return synchronized(this) {
            val i2 = instance
            if (i2 != null) {
                i2
            } else {
                val created = creator!!(arg)
                instance = created
                creator = null
                created
            }
        }
    }

}

open class Single<out T>(creator: () -> T) {
    private var creator: (() -> T)? = creator

    @Volatile
    private var instance: T? = null

    fun getInstance(): T {
        val i = instance
        if (i != null) {
            return i
        }

        return synchronized(this) {
            val i2 = instance
            if (i2 != null) {
                i2
            } else {
                val created = creator!!()
                instance = created
                creator = null
                created
            }
        }
    }

}